/*-----------------------------------------------------------------------------------------------
// Copyright (C) 2016 华跃博弈科技有限公司 
//
// 文件名：        DataReaderExtention.cs
// 文件功能描述：   IDataReader扩展
//
// 
// 创建标识：       庞永飞  2016-04-21
//
// 修改标识：
// 修改描述：
//
// 备注：	
// 				
//-----------------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Runtime.Serialization;


namespace HYBY.Task.Repository { 
  
  public static class DataReaderExtention {
    public static T SafeRead<T>(this IDataReader reader, string fieldName, T defaultValue) {
      try {
        int ordinal = reader.GetOrdinal(fieldName);
        if (reader.IsDBNull(ordinal))
          return defaultValue;
        return (T)Convert.ChangeType(reader[ordinal], defaultValue.GetType());
      }
      catch (InvalidCastException) {
        return defaultValue;
      }
    }

    public static T FillEntity<T>(this IDataReader reader) where T : class,new() {
      T RowInstance = default(T);
      if (reader.Read()) {
        RowInstance = new T();// Activator.CreateInstance<T>();
        PropertyInfo[] Properties = typeof(T).GetProperties();
        foreach (PropertyInfo property in Properties) {
          DataMemberAttribute DataMember = property.GetCustomAttribute<DataMemberAttribute>();
          if ((DataMember == null)) continue;
          string fieldName = DataMember.Name;
          try {
            //int index = reader.GetOrdinal(fieldName);
            object value = null;
            if (property.PropertyType == typeof(System.Boolean)) {
              value = reader[fieldName] == null ? false : Convert.ToBoolean(reader[fieldName]);
            }
            else {
              value = reader[fieldName];
            }
            if (value == DBNull.Value) continue;
            property.SetValue(RowInstance, value, null);
          }
          catch (Exception) {
            //throw ex;						
          }
        }
        for (int i = 0; i < Properties.Length; i++) {
          Properties[i] = null;
        }
        Properties = null;
      }
      reader.Close();
      reader.Dispose();
      return RowInstance;
    }

    public static List<T> FillGenericList<T>(this IDataReader reader) where T : class, new() {
      List<T> DataList = new List<T>();
      PropertyInfo[] Properties = typeof(T).GetProperties();
      Dictionary<String, Int32> FieldList = new Dictionary<string, int>();
      foreach (PropertyInfo property in Properties) {
        DataMemberAttribute DataMember = property.GetCustomAttribute<DataMemberAttribute>();
        if ((DataMember == null)) continue;
        string fieldName = DataMember.Name;
        try {
          int index = reader.GetOrdinal(fieldName);
          FieldList.Add(property.Name, index);
        }
        catch {
          continue;
        }
      }

      while (reader.Read()) {
        T RowInstance = new T();// Activator.CreateInstance<T>();
        foreach (PropertyInfo property in Properties) {
          //DescriptionAttribute DescriptionAttr = property.GetCustomAttribute<DescriptionAttribute>();
          //if ((DescriptionAttr == null)) continue;
          //string fieldName = DescriptionAttr.Description;
          try {
            if (!FieldList.ContainsKey(property.Name)) continue;
            int index = FieldList[property.Name];//reader.GetOrdinal(fieldName);
            object value = null;
            if (property.PropertyType == typeof(System.Boolean)) {
              value = reader[index].ToString() == "" ? false : Convert.ToBoolean(reader[index]);
            }
            else {
              value = reader[index];
            }
            if ((property.PropertyType == typeof(String)) && (value.GetType() == typeof(DBNull))) {
              value = "";
            }
            if (value == DBNull.Value) continue;
            property.SetValue(RowInstance, value, null);
          }
          catch (Exception) {
            continue;
            //throw ex;						
          }
        }
        DataList.Add(RowInstance);
      }
      for (int i = 0; i < Properties.Length; i++) {
        Properties[i] = null;
      }
      Properties = null;
      reader.Close();
      reader.Dispose();
      return DataList;
    }
  }
}